layui.use(['layer'],function () {
    var $ = layui.jquery,
        layer = layui.layer;
    $(".person_box").click(function () {
        window.location.href = "../home/setting"
    })
    /*
              获取登录用户信息
           */
    $("#sign_out").click(function () {
        sessionStorage.clear()
        window.location.reload()
    })
    if(typeof (sessionStorage.user) != 'undefined')
        var user =JSON.parse(sessionStorage.user);
    console.log(sessionStorage.user)
    if(user!=null){
        $(".login_btn").addClass("hide")
        $("#default_head").addClass("hide")
        $(".person_box").removeClass("hide")
        $("#sign_out").removeClass("hide")
        $("#top_nickname").html(user.nickname)
        $("#head-img").attr("src","../"+user.faceImage)
        $("#top_personal_signature").html(user.personalSignature)
    }
    var map = [];
    var course = null;
    $.ajax({
        type: "get",
        url: "../videos/",
        dataType: "json",
        data: {
            "id": sessionStorage.getItem('id')
        },
        success:function (res) {
            for(var i=0;i<res.data.length;i++) {
                var s = res.data[i].video_seconds;
                var minute = parseInt(s/60);
                var second = s%60;
                if (minute<10)minute = "0"+minute
                if (second<10)second = "0"+second
                var time = minute+":"+second;

                $(".chapter_box").append("  <div class=\"chapter_innerbox\">\n" +
                    "                            <div class=\"part\">"+res.data[i].chapter+"、<span class=\"video_title\">"+res.data[i].video_title+"</span></div>\n" +
                    "                            <div class=\"duration\">"+time+"</div>\n" +
                    "                        </div>")
                map.push({"part":res.data[i].chapter,"video_path":res.data[i].video_path})
            }
            $(".chapter_innerbox").click(function () {
                var a = $(this).children(".part").html().substr(0,1);
                var index = map.findIndex(function(o){
                    return o.part == a
                })
                console.log(map[index].video_path)
                $("#video").attr("src","../"+map[index].video_path)
            })
        }
    })

    var approve_flag = null;//点赞标识符
    $("#img_good").click(function () {
        if(approve_flag=='approve'){
            $.ajax({
                type:"get",
                url:"../approve/delete",
                dataType:"json",
                data:{
                    "userId":user.id,
                    "courseId":course.id
                },
                success:function (res) {
                    console.log(res)
                    approve_flag = "unapprove"
                    var value = $("#course_likes").html()*1-1;
                    $("#course_likes").html(value)
                    $("#img_good").css("background-image","url(../img/点赞.png)")
                    $.ajax({
                        type:"get",
                        url:"../course/updatelikes",
                        dataType:"json",
                        data:{
                            "id":course.id,
                            "courseLikes":value
                        }
                    })
                }
            })

        }else{
            $.ajax({
                type:"get",
                url:"../approve/insert",
                dataType:"json",
                data:{
                    "userId":user.id,
                    "courseId":course.id
                },
                success:function (res) {
                    console.log(res)
                    var value = $("#course_likes").html()*1+1;
                    approve_flag = "approve"
                    $("#course_likes").html(value)
                    $("#img_good").css("background-image","url(../img/点赞-选中.png)")
                    $.ajax({
                        type:"get",
                        url:"../course/updatelikes",
                        dataType:"json",
                        data:{
                            "id":course.id,
                            "courseLikes":value
                        }
                    })
                }
            })
        }

    })
    
    
    $.ajax({
        type:"get",
        url:"../course/",
        dataType:"json",
        data:{
            "id":sessionStorage.getItem('id')
        },
        success:function (res) {
            console.log(res)
            course = res.data;
            $("#course_title").html(res.data.course_title)
            $("#playtimes").html(res.data.course_looks)
            $("#time").html(res.data.create_time)
            $("#course_likes").html(res.data.course_likes)
            $("#course_reserves").html(res.data.course_reserves)
            $("#course_desc").html(res.data.course_desc)
            $("#nickname").html(res.data.nickname)
            $("#personal_signature").html(res.data.personal_signature)
            $(".head-img").attr("src","../"+res.data.face_image)
            $.ajax({
                type:"get",
                url:"../comments/",
                dataType:"json",
                data:{
                    "id":sessionStorage.getItem('id')
                },
                success:function (res) {
                    for(var i=0;i<res.data.length;i++){
                        $("#comments").append(" <div class=\"comment_box\">\n" +
                            "                                <img src=\"../"+res.data[i].face_image+"\" class=\"head-img\" >\n" +
                            "                                <div>\n" +
                            "                                    <div class=\"nickname\">"+res.data[i].nickname+"</div>\n" +
                            "                                    <div class=\"comment\">"+res.data[i].comment+"</div>\n" +
                            "                                    <div class=\"util\">\n" +
                            "                                        <span class=\"comment_time\">"+res.data[i].create_time+"</span>\n" +
                            "                                        <img src=\"../img/comment_approve.png\" id=\"comment_approve_img\" />\n" +
                            "                                        <span class=\"comment_approve\">"+res.data[i].approvers+"</span>\n" +
                            "                                    </div>\n" +
                            "                                </div>\n" +
                            "                            </div>")
                    }

                }
            })
            //==========================获取点赞信息=================================
            $.ajax({
                type: "get",
                url: "../comments/approves",
                dataType: "json",
                data:{
                    "userId":user.id,
                    "courseId":course.id
                },
                success:function (res) {
                    console.log(res)
                    if (res.code!='999999'){
                        approve_flag = 'approve'
                        $("#img_good").css("background-image","url(../img/点赞-选中.png)")
                    }else{
                        approve_flag = 'unapprove'
                        $("#img_good").css("background-image","url(../img/点赞.png)")
                    }
                }
            })
        }
    })
    $("#send_btn").click(function () {
        var comment = $("#mycomment").val();
        if(comment!=='')
        $.ajax({
            type: "get",
            url: "../comments/sendComment",
            dataType: "json",
            data: {
                "courseId":course.id,
                "userId":user.id,
                "approvers":0,
                "comment":comment,
            },
            success:function (res) {

                $("#mycomment").val("")
                $("#comments").empty();
                layer.msg("发送成功！")
                $.ajax({
                    type:"get",
                    url:"../comments/",
                    dataType:"json",
                    data:{
                        "id":sessionStorage.getItem('id')
                    },
                    success:function (res) {
                        for(var i=0;i<res.data.length;i++){
                            $("#comments").append(" <div class=\"comment_box\">\n" +
                                "                                <img src=\"../"+res.data[i].face_image+"\" class=\"head-img\" >\n" +
                                "                                <div>\n" +
                                "                                    <div class=\"nickname\">"+res.data[i].nickname+"</div>\n" +
                                "                                    <div class=\"comment\">"+res.data[i].comment+"</div>\n" +
                                "                                    <div class=\"util\">\n" +
                                "                                        <span class=\"comment_time\">"+res.data[i].create_time+"</span>\n" +
                                "                                        <img src=\"../img/comment_approve.png\" id=\"comment_approve_img\" />\n" +
                                "                                        <span class=\"comment_approve\">"+res.data[i].approvers+"</span>\n" +
                                "                                    </div>\n" +
                                "                                </div>\n" +
                                "                            </div>")
                        }

                    }
                })

            }
        })
        else{
            layer.error("请输入评论信息！")
        }
    })


})